package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Person;
import com.example.springboot.entity.PersonR;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.PersonMapper;
import com.example.springboot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserMapper userMapper;
    @Resource
    PersonMapper personMapper;


    @PostMapping("/insert")
    public Result<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        User res = userMapper.selectOne(wrapper);
        if (res == null) {
            return Result.error("-1","用户名或者密码错误");
        }
        return Result.success(res);
    }
    @PostMapping("/register")
    public Result<?> register(@RequestBody PersonR personR) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, personR.getUsername());
        User res = userMapper.selectOne(wrapper);
        if (res != null) {
            return Result.error("-1","用户名重复");
        }
        User user = new User();
        user.setUsername(personR.getUsername());
        user.setPassword(personR.getPassword());
        userMapper.insert(user);
        wrapper = Wrappers.<User>lambdaQuery()
                   .eq(User::getUsername, personR.getUsername());
        user = userMapper.selectOne(wrapper);
        Person person = new Person();
        person.setUid(user.getUid());
        person.setUsername(personR.getUsername());
        person.setSex(personR.getSex());
        person.setAge(personR.getAge());
        person.setEmail(personR.getEmail());
        personMapper.insert(person);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(User::getUsername,search);
        }
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

}
